package com.etone.smartAudit.commService;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etone.smartAudit.domain.PLogVersion;
import com.etone.smartAudit.domain.PUumOrganizationinfo;
import com.etone.smartAudit.dto.PageDTO;
import com.etone.smartAudit.mapper.PUumOrganizationinfoMapper;
import com.etone.smartAudit.vo.PUumOrgVO;
import com.etone.smartAudit.vo.PortalBaseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author jiayen
 * @since 2021-12-07 11:58:05
 */
@Slf4j
@Service
public class CommPUumOrganizationinfoService extends ServiceImpl<PUumOrganizationinfoMapper, PUumOrganizationinfo> {


    @Value("${uds.level:2}")
    private Integer ouLevel;

    /***
     * 添加进组织
     */
    public void addOrganization(PUumOrganizationinfo org) {
        //判断该组织是否已经存在， 存在则更新
        String oUGUID = org.getOUGUID();
        LambdaQueryWrapper<PUumOrganizationinfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PUumOrganizationinfo::getOUGUID, oUGUID);
        PUumOrganizationinfo exsitOrg = this.getOne(queryWrapper);
        if (exsitOrg != null) {
            //更新
            this.update(org, queryWrapper);
        } else {
            //添加
            this.save(org);
        }
    }

    /**
     * 获取portal组织架构树形结构
     *
     * @return
     */
    public List<PUumOrganizationinfo> getListTree(PageDTO pageDTO) {

        return null;
    }

    /**
     * 获取组织树
     *
     * @return
     */
    public List<PUumOrgVO> getOrgTree() {

        LambdaQueryWrapper<PUumOrganizationinfo> lambdaQueryWrapper = getOrgSelect();
        List<PUumOrganizationinfo> list = this.list(lambdaQueryWrapper);
        List<PUumOrgVO> result = new ArrayList<>();

        List<PUumOrgVO> firstList = new ArrayList<>();
        //获取顶层组织
        list.stream().forEach(org -> {
            PUumOrgVO vo = new PUumOrgVO();
            BeanUtils.copyProperties(org, vo);
            result.add(vo);
            if (ouLevel.equals(vo.getOULevel())) {
                firstList.add(vo);
            }
        });
        handorgTree(result, firstList);
        return firstList;
    }


    /**
     * 获取组织树
     *
     * @param list
     * @param firstList
     */
    public void handorgTree(List<PUumOrgVO> list, List<PUumOrgVO> firstList) {
        firstList.stream().forEach(porg -> {
            List<PUumOrgVO> children = porg.getChildren();
            String id = porg.getOUGUID();
            if (children == null) {
                children = new ArrayList<>();
                porg.setChildren(children);
            }
            list.stream().forEach(child -> {
                String parentId = child.getParentOUGUID();
                if (parentId != null && parentId.equals(id)) {
                    porg.getChildren().add(child);
                }
            });

            if (CollectionUtil.isNotEmpty(porg.getChildren())) {
                handorgTree(list, porg.getChildren());
            }
        });
    }

    /***
     * 查询数据
     * @return
     */
    private LambdaQueryWrapper<PUumOrganizationinfo> getOrgSelect() {
        LambdaQueryWrapper<PUumOrganizationinfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(PUumOrganizationinfo::getOUGUID, PUumOrganizationinfo::getParentOUGUID
                , PUumOrganizationinfo::getOUName, PUumOrganizationinfo::getOUFullName, PUumOrganizationinfo::getOrgDN
                , PUumOrganizationinfo::getRegion, PUumOrganizationinfo::getOULevel
                ,PUumOrganizationinfo::getCompanyID,PUumOrganizationinfo::getCompany);
        lambdaQueryWrapper.eq(PUumOrganizationinfo::getOrgState, 1);
        return lambdaQueryWrapper;
    }

    /***
     * 根据父级获取子级列表
     * @param parentId
     * @return
     */
    public List<PUumOrgVO> getOrgList(String parentId) {
        LambdaQueryWrapper<PUumOrganizationinfo> lambdaQueryWrapper = getOrgSelect();
        if (StrUtil.isEmpty(parentId)) {
            lambdaQueryWrapper.eq(PUumOrganizationinfo::getOULevel, ouLevel);
        } else {
            lambdaQueryWrapper.eq(PUumOrganizationinfo::getParentOUGUID, parentId);
        }
        List<PUumOrganizationinfo> list = this.list(lambdaQueryWrapper);
        List<PUumOrgVO> result = new ArrayList<>();
        list.stream().forEach(org -> {
            PUumOrgVO vo = new PUumOrgVO();
            BeanUtils.copyProperties(org, vo);
            vo.setCompanyId(org.getCompanyID());
            result.add(vo);
        });
        return result;
    }

    /***
     * 获取所有组织
     * @return
     */
    public Map<String, PUumOrgVO> getAllOrgs() {
        LambdaQueryWrapper<PUumOrganizationinfo> lambdaQueryWrapper = getOrgSelect();
        List<PUumOrganizationinfo> list = this.list(lambdaQueryWrapper);
        Map<String, PUumOrgVO> map = new HashMap<>();
        list.stream().forEach(org -> {
            PUumOrgVO vo = new PUumOrgVO();
            BeanUtils.copyProperties(org, vo);
            map.put(vo.getOUGUID(), vo);
        });
//        log.debug(JSON.toJSONString(map));
        return map;
    }

    /***
     * 搜索公司
     * @param key
     * @return
     */
    public List<PUumOrgVO> searchOrgs(String key) {
        LambdaQueryWrapper<PUumOrganizationinfo> query = getOrgSelect();
        query.like(PUumOrganizationinfo::getOrgFullName, key);
        List<PUumOrganizationinfo> list = this.list(query);
        List<PUumOrgVO> voList = new ArrayList<>();
        list.stream().forEach(org -> {
            PUumOrgVO vo = new PUumOrgVO();
            BeanUtils.copyProperties(org, vo);
            voList.add(vo);
        });
        return voList;
    }

    public PUumOrgVO getOrgById(String roleId) {
        LambdaQueryWrapper<PUumOrganizationinfo> query = getOrgSelect();
        query.eq(PUumOrganizationinfo::getOUGUID,roleId);
        PUumOrganizationinfo info =  this.getOne(query);
        PUumOrgVO vo = new PUumOrgVO();
        BeanUtils.copyProperties(info, vo);
        return vo;
    }

    public List<PUumOrgVO> getAll(){
        LambdaQueryWrapper<PUumOrganizationinfo> query = getOrgSelect();
        List<PUumOrgVO> voList = new ArrayList<>();
        List<PUumOrganizationinfo> list = this.list(query);
        list.stream().forEach(org -> {
            PUumOrgVO vo = new PUumOrgVO();
            BeanUtils.copyProperties(org, vo);
            voList.add(vo);
        });
        return voList;
    }
}
